Conditions | 1 |
Paths | 1 |
Total Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | 'use strict'; |
||
13 | function($scope, $interval, $timeout, savegame, state, data, util, reaction) { |
||
14 | $scope.state = state; |
||
15 | $scope.data = data; |
||
16 | $scope.util = util; |
||
17 | |||
18 | let self = this; |
||
19 | |||
20 | self.update = function() { |
||
21 | state.reactions = []; |
||
22 | state.update(state.player); |
||
23 | reaction.processReactions(state.reactions, state.player); |
||
24 | }; |
||
25 | |||
26 | self.updateLoop = function() { |
||
27 | self.update(); |
||
28 | let speed = 1000; |
||
29 | if(state.fasterTicks){ |
||
30 | speed = 1; |
||
31 | state.player.offline--; |
||
32 | if(state.player.offline <= 0) state.fasterTicks = 0; |
||
|
|||
33 | } |
||
34 | $timeout(self.updateLoop, speed); |
||
35 | }; |
||
36 | |||
37 | self.startup = function() { |
||
38 | savegame.load(); |
||
39 | let elapsed = Math.floor(Date.now()/1000)-state.player.last_login; |
||
40 | let total = util.calculateValue(data.global_upgrades.offline_time.power.base, |
||
41 | data.global_upgrades.offline_time.power, |
||
42 | state.player.global_upgrades.offline_time); |
||
43 | // lets limit the offline elapsed time |
||
44 | state.player.offline = Math.min(total, state.player.offline+elapsed); |
||
45 | |||
46 | state.loading = false; |
||
47 | // trigger the game loop |
||
48 | $timeout(self.updateLoop, 1000); |
||
49 | $interval(savegame.save, 10000); |
||
50 | }; |
||
51 | |||
52 | $timeout(self.startup); |
||
53 | } |
||
54 | ]); |
||
55 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.